/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package navegador.tela;

import java.awt.Color;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author SergioPeres
 */
public class ClientBalancer extends javax.swing.JFrame {

    private String ip = "10.248.134.220:8081 POST";
    
    private String address;
    private int port;
    
    private String user = null;
    private String password = null;
    
    private final String host;
    private final int timeOut = 10000;
    
    /**
     * Creates new form ClinteBalancer
     */
    public ClientBalancer() {
        initComponents();
        
        host = getMacAdress();
        
        btnLogin.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPort = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        btnLogin = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtAddFriends = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        btnAddFriends = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtDelFriend = new javax.swing.JTextField();
        btnDelFriend = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtAddPhoto = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtAddMark = new javax.swing.JTextField();
        btnAddPhoto = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JSeparator();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        txtDelPhoto = new javax.swing.JTextField();
        btnDelPhoto = new javax.swing.JButton();
        jSeparator9 = new javax.swing.JSeparator();
        btnAddMark = new javax.swing.JButton();
        jLabel23 = new javax.swing.JLabel();
        txtAddMarkPhoto = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        txtFriendsOf = new javax.swing.JTextField();
        btnFriendsOf = new javax.swing.JButton();
        jSeparator6 = new javax.swing.JSeparator();
        jLabel16 = new javax.swing.JLabel();
        txtPhotoOf = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        btnPhotoFo = new javax.swing.JButton();
        jSeparator7 = new javax.swing.JSeparator();
        jLabel19 = new javax.swing.JLabel();
        txtWhoAre = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        btnWhoAre = new javax.swing.JButton();
        jSeparator8 = new javax.swing.JSeparator();
        jLabel21 = new javax.swing.JLabel();
        txtNotFriends = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        btnNotFriends = new javax.swing.JButton();
        lblMensagem = new javax.swing.JLabel();
        txtServer = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(":: Client ::");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Client");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Server: ");

        jLabel3.setText("Port: ");

        txtPort.setText("9000");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Usuario: ");

        txtUsuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUsuarioFocusGained(evt);
            }
        });

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel6.setText("Digite o nome de sus amigos aqui (separe cada amigo com um ponto e virgula [;]) para adicionalos!");

        txtAddFriends.setToolTipText("Separe cada amigo com um ponto e virgula [;]");

        jLabel7.setText("Add Friend: ");

        btnAddFriends.setText("Add");
        btnAddFriends.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFriendsActionPerformed(evt);
            }
        });

        jLabel8.setText("Digite o nome de um amigo para removelo!");

        jLabel9.setText("Del Friend: ");

        btnDelFriend.setText("Del");
        btnDelFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelFriendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtAddFriends, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAddFriends))
                            .addComponent(jLabel8)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDelFriend, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnDelFriend)))
                        .addGap(0, 166, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAddFriends, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(btnAddFriends))
                .addGap(18, 18, 18)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtDelFriend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelFriend))
                .addContainerGap(158, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Friends", jPanel1);

        jLabel10.setText("Digite o nome de uma foto para adiciona-la, e o nome de seus amigos para marcalos (os nomes dever separados por ;)");

        jLabel11.setText("Add Photo: ");

        jLabel12.setText("Marcar Amigo: ");

        btnAddPhoto.setText("add");
        btnAddPhoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddPhotoActionPerformed(evt);
            }
        });

        jLabel13.setText("Digite o nome de uma foto para apagala.");

        jLabel14.setText("Del Photo: ");

        btnDelPhoto.setText("Del");
        btnDelPhoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelPhotoActionPerformed(evt);
            }
        });

        btnAddMark.setText("Go");
        btnAddMark.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddMarkActionPerformed(evt);
            }
        });

        jLabel23.setText(" Photo: ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator5)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtAddPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAddPhoto))
                            .addComponent(jLabel13)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDelPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDelPhoto))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtAddMark, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel23)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtAddMarkPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAddMark)))
                        .addGap(0, 70, Short.MAX_VALUE))
                    .addComponent(jSeparator9))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtAddPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddPhoto))
                .addGap(18, 18, 18)
                .addComponent(jSeparator9, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtAddMark, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddMark)
                    .addComponent(jLabel23)
                    .addComponent(txtAddMarkPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtDelPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelPhoto))
                .addContainerGap(101, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Photos", jPanel2);

        jLabel15.setText("Quem são os amigos de ");

        btnFriendsOf.setText("Ir");
        btnFriendsOf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFriendsOfActionPerformed(evt);
            }
        });

        jLabel16.setText("Em quais fotos ");

        jLabel17.setText(" aparece? ");

        jLabel18.setText(" ? ");

        btnPhotoFo.setText("Ir");
        btnPhotoFo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPhotoFoActionPerformed(evt);
            }
        });

        jLabel19.setText("Quem aparece na foto ");

        jLabel20.setText(" ? ");

        btnWhoAre.setText("Ir");
        btnWhoAre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWhoAreActionPerformed(evt);
            }
        });

        jLabel21.setText("Quem são os amigos dos amigos de ");

        jLabel22.setText(" que não são seus amigos? ");

        btnNotFriends.setText("Ir");
        btnNotFriends.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNotFriendsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator6)
                    .addComponent(jSeparator7)
                    .addComponent(jSeparator8)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel16)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPhotoOf, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel17)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnPhotoFo))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtFriendsOf, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel18)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnFriendsOf))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel19)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtWhoAre, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel20)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnWhoAre))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel21)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtNotFriends, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel22)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnNotFriends)))
                        .addGap(0, 132, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(txtFriendsOf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFriendsOf)
                    .addComponent(jLabel18))
                .addGap(18, 18, 18)
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(txtPhotoOf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(btnPhotoFo))
                .addGap(18, 18, 18)
                .addComponent(jSeparator7, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(txtWhoAre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20)
                    .addComponent(btnWhoAre))
                .addGap(18, 18, 18)
                .addComponent(jSeparator8, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(txtNotFriends, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22)
                    .addComponent(btnNotFriends))
                .addContainerGap(74, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Questions", jPanel3);

        lblMensagem.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblMensagem.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtServer.setText("localhost");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1)
                    .addComponent(jSeparator2)
                    .addComponent(jSeparator3)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtServer, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnLogin)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAdd)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(lblMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLogin)
                    .addComponent(btnAdd))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtUsuarioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsuarioFocusGained
        txtUsuario.selectAll();
    }//GEN-LAST:event_txtUsuarioFocusGained

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        connect("login");
    }//GEN-LAST:event_btnLoginActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        connect("addUser");
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnAddFriendsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFriendsActionPerformed
        connect("addFriends");
    }//GEN-LAST:event_btnAddFriendsActionPerformed

    private void btnDelFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelFriendActionPerformed
        connect("delFriend");
    }//GEN-LAST:event_btnDelFriendActionPerformed

    private void btnAddPhotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddPhotoActionPerformed
        connect("addPhoto");
    }//GEN-LAST:event_btnAddPhotoActionPerformed

    private void btnDelPhotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelPhotoActionPerformed
        connect("delPhoto");
    }//GEN-LAST:event_btnDelPhotoActionPerformed

    private void btnFriendsOfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFriendsOfActionPerformed
        connect("friendsOf");
    }//GEN-LAST:event_btnFriendsOfActionPerformed

    private void btnPhotoFoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPhotoFoActionPerformed
        connect("photoFo");
    }//GEN-LAST:event_btnPhotoFoActionPerformed

    private void btnWhoAreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWhoAreActionPerformed
        connect("whoAre");
    }//GEN-LAST:event_btnWhoAreActionPerformed

    private void btnNotFriendsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNotFriendsActionPerformed
        connect("notFriends");
    }//GEN-LAST:event_btnNotFriendsActionPerformed

    private void btnAddMarkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddMarkActionPerformed
        connect("addMark");
    }//GEN-LAST:event_btnAddMarkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientBalancer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientBalancer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientBalancer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientBalancer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientBalancer().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAddFriends;
    private javax.swing.JButton btnAddMark;
    private javax.swing.JButton btnAddPhoto;
    private javax.swing.JButton btnDelFriend;
    private javax.swing.JButton btnDelPhoto;
    private javax.swing.JButton btnFriendsOf;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnNotFriends;
    private javax.swing.JButton btnPhotoFo;
    private javax.swing.JButton btnWhoAre;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblMensagem;
    private javax.swing.JTextField txtAddFriends;
    private javax.swing.JTextField txtAddMark;
    private javax.swing.JTextField txtAddMarkPhoto;
    private javax.swing.JTextField txtAddPhoto;
    private javax.swing.JTextField txtDelFriend;
    private javax.swing.JTextField txtDelPhoto;
    private javax.swing.JTextField txtFriendsOf;
    private javax.swing.JTextField txtNotFriends;
    private javax.swing.JTextField txtPhotoOf;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServer;
    private javax.swing.JTextField txtUsuario;
    private javax.swing.JTextField txtWhoAre;
    // End of variables declaration//GEN-END:variables

    
    public Parametro getParametro() {
        Parametro p = new Parametro();
        p.setUrl(txtServer.getText().trim());
        p.setPort(Integer.parseInt(txtPort.getText().trim()));
        p.setMetodo("POST");
        return p;
    }
    
    public String trasmitir(Parametro par) {
        UriRawContent uriRawContent = new UriRawContent(par);
            
        try {
            return uriRawContent.getURIRawContent();
        } catch (UnknownHostException e) {
            System.out.println("Host desconhecido!" + e);
            return "Erro: UnknownHostException!";
        } catch (IOException e) {
            System.out.println("Erro de entrada e saída!" + e);
            return "Erro: IOException!";
        }
    }
    
    public void connect(String op) {
        //address = cbServer.getSelectedItem().toString();
        //port = Integer.parseInt(txtPort.getText());
        
        //address = (address.equals("localhost") ? address : address + ".cloudapp.net");
        
        switch (op) {
            case "addUser"   : addUser();    break;
            case "addFriends": addFriends(); break;
            case "delFriend" : delFriend();  break;
            case "addPhoto"  : addPhoto();   break;
            case "delPhoto"  : delPhoto();   break;
            case "friendsOf" : friendsOf();  break;
            case "photoOf"   : photoOf();    break;
            case "whoAre"    : whoAre();     break;
            case "notFriends": notFriends(); break;
            case "addMark"   : addMark(); break;
        }
            
    }
    
    // =======================================================================
    // ======== IMPLEMENTADO 
    private void addUser() {
        mensagem("addUser....", 0);
        
        String user = txtUsuario.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("pessoa");
        p.setParams(user);
        
        String msn = trasmitir(p);
        
        if (msn != null) {
            mensagem("addUser: " + user + " Ok", 0);
        } else {
             mensagem("addUser... ERRO!", 1);
        }
    }
    
    private void addFriends() {
        mensagem("addFriends....", 0);
        
        String user = txtUsuario.getText().trim();
        String friend = txtAddFriends.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("amizade");
        p.setParams(user + "#" + friend);
        
        //ArrayList<String> friends = parserFriends(arrayFriends);
        //Menssagem msn = action.transmitir(new Menssagem("addFriends", arrayFriends));
        
        String msn = trasmitir(p);
        
        if (msn != null) {
            mensagem("addFriends.... Ok", 0);
            txtAddFriends.setText("");
        } else {
             mensagem("addFriends... ERRO!", 1);
        }
    }
    
    private void addPhoto() {
        mensagem("addPhoto....", 0);
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("foto");
        
        String foto = txtAddPhoto.getText().trim();
        //String arrayFriends = txtAddPhotoList.getText().replaceAll(";", "#");//.split("[;]+");
        p.setParams(foto);
        
        String msn = trasmitir(p);
        
        //ArrayList<String> friends = parserFriends(arrayFriends);
        //Menssagem msn = action.transmitir(new Menssagem("addPhoto", foto + "#" + arrayFriends));
        if (msn != null) {
            mensagem("addPhoto: " + foto + " Ok", 0);
            txtAddPhoto.setText("");
            txtAddMark.setText("");
        } else {
             mensagem("addPhoto... ERRO!", 1);
        }
    } 
            
    private void addMark() {
        mensagem("addMark....", 0);
        
        String user = txtAddMark.getText().trim();
        String photo = txtAddMarkPhoto.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("marcacao");
        p.setParams(user + "#" + photo);
        
        String msn = trasmitir(p);
        
        //Menssagem msn = action.transmitir(new Menssagem("notFriends", user));
        //List<String> friends = action.notFriends(user);
        if (msn != null) {
            mensagem("addMark: " + user + " Ok", 0);
            visualizarLista("Amigos dos meus amigos de " + " que não são meus amigos!", msn);
            txtAddMark.setText("");
            txtAddMarkPhoto.setText("");
        } else {
            mensagem("addMark... ERRO!", 1);
        }
    }
    
    // =======================================================================
    // ======== NAO IMPLEMENTADO
    private void delPhoto() {
        JOptionPane.showMessageDialog(null, "Não implementado!");
        /*
        mensagem("delPhoto....", 0);
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("foto");
        
        String foto = txtDelPhoto.getText().trim();
        p.setParams(address);
        
        String msn = trasmitir(p);
        
//Menssagem msn = action.transmitir(new Menssagem("delPhoto", user));
        if (msn != null) {
            mensagem("delPhoto: " + foto + " Ok", 0);
            txtDelPhoto.setText("");
        } else {
            mensagem("delPhoto... ERRO!", 1);
        } */
    } 
    
    private void delFriend() {
        JOptionPane.showMessageDialog(null, "Não implementado!");
        /*
        mensagem("delFriend....", 0);
        
        String friend = txtDelFriend.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("pessoa");
        p.setParams(friend);
        
        String msn = trasmitir(p);
        
        //Menssagem msn = action.transmitir(new Menssagem("delFriend", friend));
        if (msn != null) {
            mensagem("delFriend: " + friend + " Ok", 0);
            txtDelFriend.setText("");
        } else {
             mensagem("delFriend... ERRO!", 1);
        } */
    }
    
    
    // =======================================================================
    // ======== Perguntas
    // ----- P1 -----
    private void friendsOf() {
        mensagem("friendsOf....", 0);
        
        String user = txtFriendsOf.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("read");
        p.setType("amizade");
        p.setParams(user);
        
        String msn = trasmitir(p);
        //Menssagem msn = action.transmitir(new Menssagem("friendsOf", user));
        //List<String> friends = action.notFriends(user);
        if (msn != null && msn != null) {
            mensagem("friendsOf: " + user + " Ok", 0);
            txtFriendsOf.setText("");
        } else {
            mensagem("friendsOf... ERRO!", 1);
        }
    }
    
    // ----- P2 -----
    private void photoOf() {
        JOptionPane.showMessageDialog(null, "Não implementado!");
        /*
        mensagem("photoOf....", 0);
        
        String user = txtFriendsOf.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("pessoa");
        p.setParams(user);
        
        String msn = trasmitir(p);
        
        //Menssagem msn = action.transmitir(new Menssagem("friendsOf", user));
        //List<String> photos = action.notFriends(user);
        if (msn != null) {
            mensagem("photoOf: " + user + " Ok", 0);
            txtFriendsOf.setText("");
        } else {
            mensagem("photoOf... ERRO!", 1);
        }
        */
    }
    
    // ----- P3 -----
    private void whoAre() {
        JOptionPane.showMessageDialog(null, "Não implementado!");
        /*
        mensagem("whoAre....", 0);
        
        String user = txtWhoAre.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("pessoa");
        p.setParams(user);
        
        String msn = trasmitir(p);
        
        //Menssagem msn = action.transmitir(new Menssagem("whoAre", user));
        //List<String> friends = action.notFriends(user);
        if (msn != null) {
            mensagem("whoAre: " + user + " Ok", 0);
            txtWhoAre.setText("");
        } else {
            mensagem("whoAre... ERRO!", 1);
        }*/
    }
    
    // ----- P4 -----
    private void notFriends() {
        JOptionPane.showMessageDialog(null, "Não implementado!");
        /*
        mensagem("notFriends....", 0);
        
        String user = txtFriendsOf.getText().trim();
        
        Parametro p = getParametro();
        p.setAction("create");
        p.setType("pessoa");
        p.setParams(user);
        
        String msn = trasmitir(p);
        
        //Menssagem msn = action.transmitir(new Menssagem("notFriends", user));
        //List<String> friends = action.notFriends(user);
        if (msn != null) {
            mensagem("notFriends: " + user + " Ok", 0);
            visualizarLista("Amigos dos meus amigos de " + " que não são meus amigos!", msn);
            txtFriendsOf.setText("");
        } else {
            mensagem("notFriends... ERRO!", 1);
        } */
    } 
    
     
            
    /**
     * Obitem o MAC da maquina local.
     * 
     * Fonte: http://www.mkyong.com/java/how-to-get-mac-address-in-java/
     * @return MAC da maquina local.
     */
    public static String getMacAdress() {
        String r = "";

        InetAddress ip;
        try {

            ip = InetAddress.getLocalHost();
            System.out.println("Current IP address : " + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            System.out.print("Current MAC address : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            System.out.println(sb.toString());
            r = sb.toString();

        } catch (UnknownHostException | SocketException e) {
            System.out.println("Erro ao tentar concultar o MAC!!!");
        }

        return r;
    }
    
    private ArrayList<String> parserFriends(String friends) {
        String[] arrayFriends = friends.split("[;]+");
        
        ArrayList<String> lsFriends = new ArrayList<String>();
        for (int i = 0; i < arrayFriends.length; i++) {
            lsFriends.add(arrayFriends[i].trim());
        }
        return lsFriends;
    }
    
    private void mensagem(String msn, int cor) {
        Color color[] = {Color.BLACK, Color.RED};
        System.out.println(">>> " + msn);
        lblMensagem.setForeground(color[cor]);
        lblMensagem.setText("  " + msn);
    }

    private void visualizarLista(String titulo, List<String> friends) {
        new ViewList(titulo, friends);
    }
    
    private void visualizarLista(String titulo, String friends) {
        new ViewList(titulo, parserFriends(friends));
    }
}
